<?php

/**
 * ventareservacionesunion actions.
 *
 * @package    SistemaRutasEc
 * @subpackage ventareservacionesunion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ventareservacionesunionActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        if ($request->isMethod('POST')) {
            $fecha = date('Y-m-d', mktime(0, 0, 0, date('n'), date('d'), date('Y')));
            $this->idco = Doctrine_Query::create()
                    ->select('c.id_co')
                    ->from('Cooperativa c')
                    ->where('c.nombre = ?', "Union Cariamanga")
                    ->andWhere('c.activo = ?', 1)
                    ->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
            $reservacions = Doctrine_Core::getTable('boleto')
                    ->createQuery('b')
                    ->where('b.dni = ?', $request->getParameter('ci'))
                    ->andWhere('b.origen = ?', "RES")
                    ->andWhere('b.estado = ?', "RES")
                    ->execute();
            
            $this->reservaciones=new ArrayObject();
             foreach ($reservacions as $item):
                $turno = Doctrine_Query::create()->from('turno t')->where('t.id_tu = ?', $item->id_tu)->fetchOne();
                if ($turno->id_co == $this->idco) {
                    if ($turno->fecha > $fecha) {
                        $this->reservaciones[] = $item;
                    } else if ($turno->fecha == $fecha) {
                        if (date('H:i:s') - $turno->hora >= 2) {
                            $this->reservaciones[] = $item;
                        }
                    }
                }
            endforeach;
            
            
            
            $this->persona = Doctrine_Core::getTable('persona')
                    ->createQuery('r')
                    ->where('r.dni = ?', $request->getParameter('ci'))
                    ->fetchOne();
            
        }

        
        
        

//        $fecha = date('Y-m-d', mktime(0, 0, 0, date('n'), date('d'), date('Y')));
//        $turnos = Doctrine::getTable('turno')
//                ->createQuery('t')
//                ->where('t.id_co = ?', $this->idco)
//                ->andWhere('t.fecha >= ?', $fecha)
//                ->execute();
//        $this->boletostotales = new ArrayObject();
//        foreach ($turnos as $tus):
//            $boletos = Doctrine::getTable('boleto')
//                    ->createQuery('t')
//                    ->where('t.id_tu = ?', $tus->getIdTu())
//                    ->execute();
//            foreach ($boletos as $bo):
//                $this->boletostotales[] = $bo;
//            endforeach;
//        endforeach;
    }

    public function executeCancelar(sfWebRequest $request) {
        
    }

    public function executeShow(sfWebRequest $request) {
        $this->boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo')));
        $this->forward404Unless($this->boleto);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new boletoForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new boletoForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        //$this->forward404Unless($boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo'))), sprintf('Object boleto does not exist (%s).', $request->getParameter('id_bo')));
        //$this->form = new boletoForm($boleto);
        $this->boleto = Doctrine_Core::getTable('boleto')
                ->createQuery('b')
                ->where('b.id_bo = ?', $request->getParameter('id_bo'))
                ->fetchOne();
        $this->persona = Doctrine_Core::getTable('persona')
                ->createQuery('r')
                ->where('r.dni = ?', $this->boleto->dni)
                ->fetchOne();

        $this->bus = Doctrine_Core::getTable('bus')
                ->createQuery('r')
                ->where('r.id_bu = ?', $this->boleto->getTurno()->id_bu)
                ->fetchOne();

        $asientos = Doctrine_Core::getTable('asiento')
                ->createQuery('a')
                ->where('a.id_bo = ?', $this->boleto->id_bo)
                ->execute();
        
        $this->asie="-";
        foreach ($asientos as $itemas):
            $this->asie = $this->asie . $itemas->numero . "-";
            
        endforeach;
        $this->vtotal = count($asientos) * $this->boleto->valor;
        $this->boleto->setEstado(Boleto::VENDIDO);
        $this->boleto->save();
        
        //$this->redirect('ventareservacionesunion/index');
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo'))), sprintf('Object boleto does not exist (%s).', $request->getParameter('id_bo')));
        $this->form = new boletoForm($boleto);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($boleto = Doctrine_Core::getTable('boleto')->find(array($request->getParameter('id_bo'))), sprintf('Object boleto does not exist (%s).', $request->getParameter('id_bo')));
        $boleto->delete();

        $this->redirect('ventareservacionesunion/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $boleto = $form->save();

            $this->redirect('ventareservacionesunion/edit?id_bo=' . $boleto->getIdBo());
        }
    }

}
